﻿/// <reference path="../../../Proggy/Scripts/jquery-1.7.2-vsdoc.js" />
/// <reference path="../../../Proggy/Content/Scripts/ProggyCore.js" />
/// <reference path="../../../Proggy/Content/Scripts/Proggy.SiteMap.js" />
/// <reference path="Proggy.CorePropertyTypes.js" />


// Content Picker Class
Proggy.CorePropertyTypes.ContentPicker = function () {

    // variables
    var idOfArea;
    var area;
    var pickerId;
    var propName;
    var inputsArea;
    var itemsArea;

    // get input object
    var getInputObj = function (id, index) {

        // add to inputs
        return jQuery(
                            "<input id=\"" + propName + "_ContentIds_" +
                            index + "_\" name=\"" + propName + ".ContentIds[" +
                            index + "]\" type=\"hidden\" value=\"" + id + "\" />"
        );

    };

    // reset inputs
    var resetInputs = function (callback) {

        // get items
        var items = itemsArea.find("li.proggy-cp-item");

        // kill inputs
        inputsArea.html("");

        // iterate
        items.each(function (i, el) {

            // get id
            var id = jQuery(el).data("nodeid");

            // add to inputs
            inputsArea.append(getInputObj(id, i));

        });

        // check
        if (callback != undefined && typeof (callback) == "function") {

            // callback
            callback();

        };

    };

    // add new button
    var bindAddNew = function () {

        // get button
        var button = jQuery("div.proggy-rp-addnew").find("a:first");

        // lb
        var lb;
        var lbInner

        // bind
        button.click(function (e) {

            // load
            Proggy.Static.InitialLoadIntoLightbox(button, e, function (lb_, lbInner_) {

                // set
                lb = lb_;
                lbInner = lbInner_;

            });

        });

        // add to before show
        Proggy.Static.AddToBeforeLightboxContentShowMethods(function () {

            Proggy.Static.WaitForTypesToRegister(function () {

                //
                var siteMap = new Proggy.SiteMap();

                //
                siteMap.Init(pickerId, false, false, function (clickedItem) {

                    // get lightbox
                    Proggy.Static.ShowTempLoader(lb, lbInner, function () {

                        // get id
                        var id = clickedItem.data("nodeid");

                        // get inputs
                        var inputs = inputsArea.find("input");

                        // get size
                        var iSize = inputs.size();

                        // add
                        inputs.push(getInputObj(id, iSize));

                        // start array
                        var mapForAction = {

                        };

                        // loop
                        inputs.each(function (i, el) {

                            // add id
                            mapForAction[i] = jQuery(el).val();

                        });

                        // data
                        var data = {
                            items: mapForAction
                        };

                        // get
                        jQuery.post(
                            window.ROOT_URL + "ProggyCore/ContentPicker/ContentPickerItems", data,
                            function (r, s, x) {

                                // get response
                                var response = jQuery(r);

                                // replace
                                itemsArea.find("ul").remove();
                                itemsArea.append(response);

                                // bind
                                bindDelete();

                                // reset inputs
                                resetInputs(function () {

                                    // kill
                                    Proggy.Static.HideLoader(true, lb, lbInner);

                                });

                            });




                    });

                });

            });

        });

    };

    // delete item
    var bindDelete = function () {

        // get items
        var items = area.find("li.proggy-cp-item");

        // iterate
        items.each(function (i, el) {

            // get 
            var theItem = jQuery(el);

            // get link
            var link = theItem.children("a.proggy-cp-item-remove:first");

            // unbind
            link.unbind("click");

            // bind
            link.click(function (e) {

                // 
                e.preventDefault();

                // check
                if (confirm("Are you sure you want to remove this item? (You will still have to save your changes)")) {

                    // delete item
                    theItem.remove();

                    // redo inputs
                    resetInputs();

                };

            });

        });

    };

    // bind move
    var bindMove = function () {

        // bind
        itemsArea.sortable({

            items: "li",
            axis: "y",
            opacity: 0.7,
            cursor: "move",
            stop: function (e, ui) {
                resetInputs();
            },
            tolerance: "pointer"
        });

    };

    // init
    this.Init = function (idOfPickerArea, pickerRequestId, propNameStart) {

        // set
        idOfArea = idOfPickerArea;
        pickerId = pickerRequestId;
        propName = propNameStart;

        // set area
        area = jQuery("#" + idOfArea);
        
        // set inputs
        inputsArea = area.find("div.proggy-rp-inputs:first");

        // set items
        itemsArea = area.find("div.proggy-rp-added:first");

        // bind add new
        bindAddNew();

        // bind delete
        bindDelete();

        // bind move up and down
        bindMove();
    };

};